package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.api.MomentsApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.pojo.User;
import com.tanhua.server.pojo.UserFreeze;
import com.tanhua.server.pojo.UserInfo;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.Comments;
import com.tanhua.server.vo.PageResult;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName :  CommentsService
 * @Author : Tenebrous
 * @Date: 2020/12/14 17:58
 * @Description : 评论的service层
 */
@Service
public class CommentsService {

    @Reference(version = "1.0.0")
    private MomentsApi momentsApi;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private UserFreezeService userFreezeService;

    /**
     * 1、查询评论列表数据
     * @param publishId         动态id
     * @param page              页码
     * @param pageSize          每页条数
     * @return                  分页参数
     */
    public PageResult queryCommentsList(String publishId, Integer page, Integer pageSize) {
        User user = UserThreadLocal.get();
        // 创建分页对象，设置分页信息
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPageSize(pageSize);
        pageResult.setCounts(0);
        pageResult.setPages(0);
        // 调用dubbo服务，查询评论列表
        PageInfo<Comment> pageInfo = this.momentsApi.queryCommentList(publishId, page, pageSize);
        // 获取评论数据列表
        List<Comment> records = pageInfo.getRecords();
        // 如果records为空
        if (CollectionUtils.isEmpty(records)) {
            // 返回pageResult，只有分页信息
            return pageResult;
        }

        // 获取用户信息
        // 定义一个userId集合，将records里面的userId存入集合中
        List<Long> userIds = new ArrayList<>();
        for (Comment record : records) {
            if (!userIds.contains(record.getUserId())) {
                userIds.add(record.getUserId());
            }
        }
        // 条件构造器
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        // 条件：user_id 在userIds集合中
        queryWrapper.in("user_id", userIds);
        // 查询userInfo表
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(queryWrapper);

        // 获取评论信息
        // 定义一个评论集合
        List<Comments> commentsList = new ArrayList<>();
        // 遍历records
        for (Comment record : records) {
            // 创建comments对象，设置参数
            Comments comments = new Comments();
            comments.setId(record.getId().toHexString());
            comments.setCreateDate(new DateTime(record.getCreated()).toString("yyyy年MM月dd日 HH:mm"));
            comments.setContent(record.getContent());
            // 遍历userInfo
            for (UserInfo userInfo : userInfoList) {
                // 如果评论userId与用户Id相同
                if(record.getUserId().longValue() == userInfo.getUserId().longValue()){
                    // 设置昵称
                    comments.setNickname(userInfo.getNickName());
                    // 设置头像
                    comments.setAvatar(userInfo.getLogo());
                    System.out.println("============================"+userInfo.getLogo());
                    break;
                }
            }

            this.movementsService.like(user, comments);

            // 添加进集合
            commentsList.add(comments);
        }
        pageResult.setItems(commentsList);
        return pageResult;
    }

    /**
     * 发表评论
     * @param publishId       动态编号
     * @param content          评论
     * @return                 状态码
     */
    public Boolean saveComments(String publishId, String content) {
        User user = UserThreadLocal.get();
        Integer type = this.userFreezeService.queryFreezingRange(user.getId());
        if (type == 2){
            return null;
        }
        return this.momentsApi.saveComment(user.getId(), publishId, 2, content);
    }
}
